package engine.operators.util;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

public class ChartUtils {

  /**
   * Creates <b> BufferedImage </b> for given JFreeChart chart. 
   * @param chart -- input chart
   * @param width -- width of BufferedImage
   * @param height -- height of BufferedImage
   * @return BufferedImage with chart
   */
  public static BufferedImage draw(JFreeChart chart, int width, int height) {
    BufferedImage img =
      new BufferedImage(width , height,
          BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();

    chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));
    g2.dispose();
    return img; 
  }
  
  public static void writeToStream(OutputStream out, BufferedImage image) throws IOException {
    ChartUtilities.writeBufferedImageAsJPEG(out, image);
  }
}
